//
//  HDCache.swift
//  Excited
//
//  Created by X140Yu on 6/14/16.
//  Copyright © 2016 X140Yu. All rights reserved.
//

import Foundation
import YYKit

class HDCache {
    static let sharedInstance = HDCache()

    private let diskCache: YYDiskCache!
    private init() {
        let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
        diskCache = YYDiskCache(path: path)
    }

    private struct Key {
        static let User = "HDUser"
        static let PYFAID = "PYFAID"
        static let PYFA = "PYFA"
        static let RealName = "RealName"
        static let Scores = "Scores"
    }

    func getPYFAID() -> String? {
        let pyfaID = diskCache.objectForKey(Key.PYFAID)
        return pyfaID as? String
    }

    func savePYFAID(ID: String) {
        diskCache.setObject(ID, forKey: Key.PYFAID)
    }

    // MARK: User
    func currentLoginUser() -> HDUser? {
        let user = diskCache.objectForKey(Key.User)
        return user as? HDUser
    }

    func saveUser(user: HDUser) {
        diskCache.setObject(user, forKey: Key.User)
    }

    // MARK: PYFA
    func getPYFA() -> HDPYFA? {
        let pyfa = diskCache.objectForKey(Key.PYFA)
        return pyfa as? HDPYFA
    }

    func savePYFA(pyfa: HDPYFA) {
        diskCache.setObject(pyfa, forKey: Key.PYFA)
    }

    // MARK: realname
    func getReadName() -> String? {
        let realName = diskCache.objectForKey(Key.RealName)
        return realName as? String
    }

    func saveRealName(realName: String) {
        diskCache.setObject(realName, forKey: Key.RealName)
    }

    // MARK: scores
    func saveScores(score: [HDScore]) {
        diskCache.setObject(score, forKey: Key.Scores)
    }

    func getScores() -> [HDScore]? {
        let scores = diskCache.objectForKey(Key.Scores)
        return scores as? [HDScore]
    }
}